package com.muyu.danmo.service.impl;


import com.muyu.common.core.utils.DateUtils;
import com.muyu.common.security.utils.SecurityUtils;
import com.muyu.common.system.domain.LoginUser;
import com.muyu.danmo.domain.DanmoTrainingShoppingProducts;
import com.muyu.danmo.domain.DanmoTrainingShoppingProductsCarousel;
import com.muyu.danmo.domain.DanmoTrainingShoppingProductsPicture;
import com.muyu.danmo.mapper.DanmoTrainingShoppingProductsCarouselMapper;
import com.muyu.danmo.mapper.DanmoTrainingShoppingProductsMapper;
import com.muyu.danmo.mapper.DanmoTrainingShoppingProductsPictureMapper;
import com.muyu.danmo.service.IDanmoTrainingShoppingProductsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 培训班商品Service业务层处理
 *
 * @author kunzong
 * @date 2024-11-28
 */
@Service
public class DanmoTrainingShoppingProductsServiceImpl implements IDanmoTrainingShoppingProductsService {
    @Autowired
    private DanmoTrainingShoppingProductsMapper danmoTrainingShoppingProductsMapper;

    @Autowired
    private DanmoTrainingShoppingProductsPictureMapper danmoTrainingShoppingProductsPictureMapper;

    @Autowired
    private static DanmoTrainingShoppingProductsCarouselMapper danmoTrainingShoppingProductsCarouselMapper;

    /**
     * 查询培训班商品
     *
     * @param id 培训班商品主键
     * @return 培训班商品
     */
    @Override
    public DanmoTrainingShoppingProducts selectDanmoTrainingShoppingProductsById(Long id) {
        return danmoTrainingShoppingProductsMapper.selectDanmoTrainingShoppingProductsById(id);
    }

    /**
     * 查询培训班商品列表
     *
     * @param danmoTrainingShoppingProducts 培训班商品
     * @return 培训班商品
     */
    @Override
    public List<DanmoTrainingShoppingProducts> selectDanmoTrainingShoppingProductsList(DanmoTrainingShoppingProducts danmoTrainingShoppingProducts) {
        return danmoTrainingShoppingProductsMapper.selectDanmoTrainingShoppingProductsList(danmoTrainingShoppingProducts);
    }

    /**
     * 新增培训班商品
     *
     * @param danmoTrainingShoppingProducts 培训班商品
     * @return 结果
     */
    @Override
    public int insertDanmoTrainingShoppingProducts(DanmoTrainingShoppingProducts danmoTrainingShoppingProducts) {
        danmoTrainingShoppingProducts.setCreateTime(DateUtils.getNowDate());
        danmoTrainingShoppingProducts.setStock(0L);
        danmoTrainingShoppingProducts.setSales(0L);
        danmoTrainingShoppingProducts.setState("展示中");
        danmoTrainingShoppingProductsMapper.insertDanmoTrainingShoppingProducts(danmoTrainingShoppingProducts);
        TrainingShoppingProductsCarouselAdd(danmoTrainingShoppingProducts);
        TrainingShoppingProductsPictureAdd(danmoTrainingShoppingProducts);
        return 1;
    }

    /**
     * 新增培训班商品图片
     * @param danmoTrainingShoppingProducts
     */
    private void TrainingShoppingProductsPictureAdd(DanmoTrainingShoppingProducts danmoTrainingShoppingProducts) {
        for (DanmoTrainingShoppingProductsPicture danmoTrainingShoppingProductsPicture : danmoTrainingShoppingProducts.getDanmoTrainingShoppingProductsPictureList()) {
            danmoTrainingShoppingProductsPicture.setProductId(danmoTrainingShoppingProducts.getId());
            danmoTrainingShoppingProductsPictureMapper.insertDanmoTrainingShoppingProductsPicture(danmoTrainingShoppingProductsPicture);
        }
    }

    /**
     * 新增培训班轮播图图片
     * @param danmoTrainingShoppingProducts
     */
    private static void TrainingShoppingProductsCarouselAdd(DanmoTrainingShoppingProducts danmoTrainingShoppingProducts) {
        for (DanmoTrainingShoppingProductsCarousel danmoTrainingShoppingProductsCarousel : danmoTrainingShoppingProducts.getDanmoTrainingShoppingProductsCarouselList()) {
            danmoTrainingShoppingProductsCarousel.setProductId(danmoTrainingShoppingProducts.getId());
            danmoTrainingShoppingProductsCarouselMapper.insertDanmoTrainingShoppingProductsCarousel(danmoTrainingShoppingProductsCarousel);
        }
    }

    /**
     * 修改培训班商品
     *
     * @param danmoTrainingShoppingProducts 培训班商品
     * @return 结果
     */
    @Override
    public int updateDanmoTrainingShoppingProducts(DanmoTrainingShoppingProducts danmoTrainingShoppingProducts) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        String userid = String.valueOf(loginUser.getUserid());
        DanmoTrainingShoppingProducts danmoTrainingShoppingProducts1 = danmoTrainingShoppingProductsMapper.selectDanmoTrainingShoppingProductsById(danmoTrainingShoppingProducts.getId());
        if (!danmoTrainingShoppingProducts1.getCreateBy().equals(userid)) {
            return 2;
        }
        danmoTrainingShoppingProducts.setUpdateBy(userid);
        danmoTrainingShoppingProducts.setUpdateTime(DateUtils.getNowDate());

        danmoTrainingShoppingProductsMapper.updateDanmoTrainingShoppingProducts(danmoTrainingShoppingProducts);
//        先删除旧数据
        TrainingShoppingProductsCarouselDelete(danmoTrainingShoppingProducts);
        TrainingShoppingProductsPictureDelete(danmoTrainingShoppingProducts);

//        添加新数据
        TrainingShoppingProductsCarouselAdd(danmoTrainingShoppingProducts);
        TrainingShoppingProductsPictureAdd(danmoTrainingShoppingProducts);
        return 1;
    }

    /**
     * 删除培训班商品对象
     * @param danmoTrainingShoppingProducts
     */
    private void TrainingShoppingProductsPictureDelete(DanmoTrainingShoppingProducts danmoTrainingShoppingProducts) {
        danmoTrainingShoppingProductsPictureMapper.deleteDanmoTrainingShoppingProductsPictureByProductId(danmoTrainingShoppingProducts.getId());
    }

    /**
     * 删除培训班轮播图对象
     * @param danmoTrainingShoppingProducts
     */
    private void TrainingShoppingProductsCarouselDelete(DanmoTrainingShoppingProducts danmoTrainingShoppingProducts) {
        danmoTrainingShoppingProductsCarouselMapper.deleteDanmoTrainingShoppingProductsCarouselByProductId(danmoTrainingShoppingProducts.getId());
    }

    /**
     * 批量删除培训班商品
     *
     * @param ids 需要删除的培训班商品主键
     * @return 结果
     */
    @Override
    public int deleteDanmoTrainingShoppingProductsByIds(Long[] ids) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        String userid = String.valueOf(loginUser.getUserid());
        List<DanmoTrainingShoppingProducts> danmoTrainingShoppingProducts = danmoTrainingShoppingProductsMapper.selectDanmoTrainingShoppingProductsByIds(ids);
//        判断是否为本人，并删除轮播图与商品图片信息
        for (DanmoTrainingShoppingProducts danmoTrainingShoppingProduct : danmoTrainingShoppingProducts) {
            if (!danmoTrainingShoppingProduct.getCreateBy().equals(userid)) {
                return 2;
            }
            TrainingShoppingProductsPictureDelete(danmoTrainingShoppingProduct);
            TrainingShoppingProductsCarouselDelete(danmoTrainingShoppingProduct);
        }
        danmoTrainingShoppingProductsMapper.deleteDanmoTrainingShoppingProductsByIds(ids);
        return 1;
    }

    /**
     * 删除培训班商品信息
     *
     * @param id 培训班商品主键
     * @return 结果
     */
    @Override
    public int deleteDanmoTrainingShoppingProductsById(Long id) {
        return danmoTrainingShoppingProductsMapper.deleteDanmoTrainingShoppingProductsById(id);
    }
}
